<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <TableHeader
            :buttons="['refresh', 'add', 'edit', 'delete', 'comSearch', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('Quick search placeholder', { fields: t('user.mobile') + '/' + t('user.nickname') })"
        />

        <!-- 表格 -->
        <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
        <Table ref="tableRef" />

        <!-- 表单 -->
        <PopupForm />

        <el-dialog
            @close="puser_show = false"
            width="100%"
            :model-value="puser_show"
            class="ba-upload-select-dialog"
            title="直推下级"
            :append-to-body="true"
            :destroy-on-close="true"
            top="0vh"
        >
            <parent_index :parent_id = parent_id />
        </el-dialog>


        <el-dialog
            v-model="arborescence_show"
            title="关系树状图"
            :fullscreen=true
        >

            <div>
                <div style="height: 75vh;">
                    <!--                    <div class="search-box">-->
                    <!--                        <span>精准搜索：</span>-->
                    <!--                        <input type="text" v-model="keyword" placeholder="请输入姓名或工号搜索" @keydown.enter="filter" /> <span style="color: red;font-size: 10px">&nbsp;&nbsp;输入后敲击回车键搜索</span>-->
                    <!--                    </div>-->
                    <p>十级总人数：<span style="font-weight: bold;font-size: 20px">{{user_num}}</span> 人</p>

                    <vue3-tree-org
                        ref="tree"
                        :data="arborescence_user"
                        center
                        :horizontal="false"
                        :collapsable="true"
                        :default-expand-level="10"

                    >


                    </vue3-tree-org>

                </div>
            </div>


            <template #footer>
                <div class="dialog-footer">
                    <el-button   @click="arborescence_show = false" type="info">退出</el-button>
                </div>
            </template>

        </el-dialog>

    </div>
</template>

<script setup lang="ts">
import { ref, provide } from 'vue'
import baTableClass from '/@/utils/baTable'
import PopupForm from './popupForm.vue'
import parent_index from './parent_index.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useI18n } from 'vue-i18n'
import createAxios from "/@/utils/axios";
import {ElMessage} from "element-plus";

defineOptions({
    name: 'user/user',
})

const { t } = useI18n()
const tableRef = ref()


const optButtons: OptButton[] = defaultOptButtons(['edit', 'delete'])

const parent_id = ref(-1)


const arborescence_user = ref({})
const user_num = ref(0)
const arborescence_show = ref(false);
optButtons.push(
    {
        render: 'tipButton',
        // name 是任意的
        name: 'info',
        // title 是语言翻译 key
        title: '直推下级',
        text: '',
        // type: 'warning',
        // icon: 'fa fa-search-plus icon',
        type: 'warning',
        icon: 'fa fa-list-ul',
        click(row, field) {
            parent_id.value = row.id
            puser_show.value = true;

            // baTable.getIndex()
            // baTable.mount()
            // baTable.getIndex()
        },

    },
    {
        render: 'tipButton',
        // name 是任意的
        name: 'info',
        // title 是语言翻译 key
        title: '下级关系网',
        text: '',
        // type: 'warning',
        // icon: 'fa fa-search-plus icon',
        type: 'success',
        icon: 'fa fa-sitemap',
        click(row, field) {
            baTable.form.loading = true
            baTable.table.extend!.showInfo = true
            createAxios({
                url: '/admin/user.user/subordinate?id='+row.id,
                method: 'get',
            }, {
                loading:true
            }, {
                text: '关系网生成中...'

            })
                .then((res) => {
                    arborescence_user.value = res.data.data;
                    user_num.value = res.data.user_num;

                    arborescence_show.value = true;
                    console.log('bb',arborescence_show.value)
                })
                .catch((resx) => {
                })


        },

    },
    )

const baTable = new baTableClass(
    new baTableApi('/admin/user.User/'),
    {
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: t('Id'), prop: 'id', align: 'center', operator: '=', operatorPlaceholder: t('Id'), width: 70 },
            // { label: t('user.User name'), prop: 'username', align: 'center', operator: 'LIKE', operatorPlaceholder: t('Fuzzy query') },
            { label: t('user.nickname'),width:130, prop: 'nickname', align: 'center', operator: 'LIKE', operatorPlaceholder: t('Fuzzy query') },
            { label: t('user.parent_id'),width:100, prop: 'parent_id', align: 'center', operator: 'eq' },
            { label: t('user.p_nickname'),width:130, prop: 'puser.nickname', align: 'center', operator: 'LIKE', operatorPlaceholder: t('Fuzzy query') },
            // {
            //     label: t('user.grouping'),
            //     prop: 'group.name',
            //     align: 'center',
            //     operator: 'LIKE',
            //     operatorPlaceholder: t('Fuzzy query'),
            //     render: 'tag',
            // },
            { label: t('user.head portrait'), prop: 'avatar', align: 'center', render: 'image', operator: false },
            // {
            //     label: t('user.Gender'),
            //     prop: 'gender',
            //     align: 'center',
            //     render: 'tag',
            //     custom: { '0': 'info', '1': '', '2': 'success' },
            //     replaceValue: { '0': t('Unknown'), '1': t('user.male'), '2': t('user.female') },
            // },
            { label: t('user.mobile'),width:130, prop: 'mobile', align: 'center', operator: 'LIKE', operatorPlaceholder: t('Fuzzy query') },
            { label: t('user.money'),width:110, prop: 'money', align: 'center', operator: 'RANGE', sortable: 'custom'},
            // { label: t('user.coin_num'),width:110, prop: 'coin_num', align: 'center', operator: 'RANGE', sortable: 'custom'},
            // { label: t('user.coin_money'),width:110, prop: 'coin_money', align: 'center', operator: 'RANGE', sortable: 'custom'},
            // { label: t('user.back_buy_money'),width:110, prop: 'back_buy_money', align: 'center', operator: 'RANGE', sortable: 'custom'},
            // { label: t('user.back_buy_money_residue'),width:110, prop: 'back_buy_money_residue', align: 'center', operator: 'RANGE', sortable: 'custom'},
            // { label: t('user.directly_return_money'),width:110, prop: 'directly_return_money', align: 'center', operator: 'RANGE', sortable: 'custom'},
            // { label: t('user.push_money'),width:110, prop: 'push_money', align: 'center', operator: 'RANGE', sortable: 'custom'},
            // { label: t('user.shop_money'),width:110, prop: 'shop_money', align: 'center', operator: 'RANGE', sortable: 'custom'},
            // { label: t('user.shareholder_money'),width:110, prop: 'shareholder_money', align: 'center', operator: 'RANGE', sortable: 'custom'},
            // { label: t('user.ten_money'),width:110, prop: 'ten_money', align: 'center', operator: 'RANGE', sortable: 'custom'},
            // { label: t('user.hundred_money'),width:110, prop: 'hundred_money', align: 'center', operator: 'RANGE', sortable: 'custom'},
            // {
            //     label: t('user.Last login IP'),
            //     prop: 'last_login_ip',
            //     align: 'center',
            //     operator: 'LIKE',
            //     operatorPlaceholder: t('Fuzzy query'),
            //     render: 'tag',
            // },
            // {
            //     label: t('user.Last login'),
            //     prop: 'last_login_time',
            //     align: 'center',
            //     render: 'datetime',
            //     sortable: 'custom',
            //     operator: 'RANGE',
            //     width: 160,
            // },
            // {
            //     label: t('user.vip_status'),
            //     prop: 'vip_status',
            //     align: 'center',
            //     render: 'tag',
            //     custom: { 0: 'danger', 1: 'success' },
            //     replaceValue: { 0: t('user.vip_status 0'), 1: t('user.vip_status 1') },
            // },
            {
                label: t('State'),
                prop: 'status',
                align: 'center',
                render: 'tag',
                custom: { disable: 'danger', enable: 'success' },
                replaceValue: { disable: t('Disable'), enable: t('Enable') },
            },
            { label: t('Create time'), prop: 'create_time', align: 'center', render: 'datetime', sortable: 'custom', operator: 'RANGE', width: 160 },

            {
                label: t('Operate'),
                align: 'center',
                width: '120',
                render: 'buttons',
                buttons: optButtons,
                operator: false,
                fixed: 'right'
            },
        ],
        dblClickNotEditColumn: [undefined],
    },
    {
        defaultItems: {
            gender: 0,
            money: '0',
            score: '0',
            status: 'enable',
        },
    }
)

interface Props {
    user_type:number
}
const props = withDefaults(defineProps<Props>(), {
    user_type: 0,
})



// 携带参数
baTable.table.filter.user_type = props.user_type

baTable.mount()
baTable.getIndex()

const puser_show = ref(false);
provide('baTable', baTable)


</script>

<style scoped lang="scss"></style>
